var deptId = ""


layui.use(['table', 'admin', 'ax', 'func', 'layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var layer = layui.layer;


    $(function () {
        //获取当前用户权限
        var ajax = new $ax(Feng.ctxPath + "/databaseListKh/getUser");
        var result = ajax.start()
        // console.log(result)
        deptId = result.data.deptId;
        getInterfaceList();
        firstClick();
    })


    /**
     * 获取接口列表,将接口名添加到菜单中
     */
    function getInterfaceList() {
        $.get(Feng.ctxPath + '/interfacelist/list', function (obj) {
            // console.log(obj)
            $("#menu").empty()
            var data = obj.data;
            for (var i = 0; i < data.length; i++) {
                var a = '<li class="layui-nav-item layui-col-md12" id=' + data[i].id + ' style="border-bottom: silver solid 1px;border-right: silver solid 1px;">';
                a += '<a href="javascript:;" style="color: black;font-size: 16px;text-align: center" id="interface">' + data[i].name + '</a>';
                a += '</li>'
                $("#menu").append(a)
            }
        })
    }


    /**
     * 第一次加载
     */
    function firstClick() {
        $.ajax(Feng.ctxPath + '/interfacelist/list',
            {
                type: "GET",
                async: false,
                success: function (obj) {
                    $("#content").empty()
                    // console.log(obj)
                    if (obj.data.length != 0) {
                        var paramArray = obj.data[0].paramId.split(",")
                        // console.log(paramArray)
                        var a = '<div class="layui-col-md12">';
                        a += '<div class="layui-col-md4">';
                        a += '<span>接口描述:</span>';
                        a += '<span>' + obj.data[0].description + '</span>';
                        a += '</div>';
                        if (deptId == 25) {
                            a += '<div class="layui-col-md4 layui-col-md-offset4 layui-inline">';
                            a += '<button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>';
                            a += '<button id="btnEdit" class="layui-btn icon-btn"><i class="layui-icon">&#xe642;</i>修改</button>';
                            a += '<button id="btnDelete" class="layui-btn icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>';
                            a += '</div>';
                        }
                        a += '<div class="layui-col-md12">';
                        a += '<span>接口地址:</span>';
                        a += '<span class="searchLoc">' + obj.data[0].loc + '</span>';
                        a += '</div>';
                        a += '</div>';
                        a += '<div class="layui-col-md12" style="margin-top: 60px">';
                        a += '<span class="layui-col-md12">参数说明</span>';
                        a += '<div class="layui-col-md7" style="margin-top: 15px">';
                        a += '<table class="layui-table">';
                        a += '<tr>';
                        a += '<td style="text-align: center;width: 35px">序号</td>';
                        a += '<td style="text-align: center">参数</td>';
                        a += '<td style="text-align: center">释义</td>';
                        a += '</tr>';
                        for (var i = 0; i < paramArray.length; i++) {
                            var id = parseInt(paramArray[i]);
                            // console.log(id)
                            $.ajax(Feng.ctxPath + '/interfaceparam/list?id=' + id, {
                                type: "GET",
                                async: false,
                                success: function (res) {
                                    // console.log(res)
                                    a += '<tr>';
                                    a += '<td style="text-align: center;width: 35px">' + (i + 1) + '</td>';
                                    a += '<td style="text-align: center">' + res.data[0].param + '</td>';
                                    a += '<td style="text-align: center">' + res.data[0].detail + '</td>';
                                    a += '</tr>';
                                }
                            })
                        }
                        a += '</table>';
                        a += '</div>';
                        a += '<div class="layui-col-md12" style="margin-top: 40px">';
                        a += '<span class="layui-col-md12">返回示例</span>';
                        a += '<textarea class="layui-col-md10" style="overflow-y:visible;border: none;background-color: white;resize:none" id="returnSample" disabled>' + obj.data[0].returnSample + '</textarea>';
                        a += '</div>';
                        a += '</div>';
                        $("#content").append(a)
                        $("#returnSample").val(obj.data[0].returnSample)
                        /**
                         * 文本域高度自适应
                         */
                        //jQuery实现textarea高度根据内容自适应
                        $.fn.extend({
                            txtaAutoHeight: function () {
                                return this.each(function () {
                                    var $this = $(this);
                                    if (!$this.attr('initAttrH')) {
                                    }
                                    setAutoHeight(this).on('input', function () {
                                        setAutoHeight(this);
                                    });
                                });

                                function setAutoHeight(elem) {
                                    var $obj = $(elem);
                                    return $obj.css({
                                        height: $obj.attr('initAttrH'),
                                        'overflow-y': 'hidden'
                                    }).height(elem.scrollHeight);
                                }
                            }
                        });
                        //调用
                        $(function () {
                            $("#returnSample").txtaAutoHeight();
                        });
                    }
                }
            })
    }


    /**
     * 点击导航变换body数据
     */
    $("#menu").on('click', '#interface', function () {
        var id = $(this).parent().attr("id")
        // console.log(id)
        $("#content").empty()
        $.ajax(Feng.ctxPath + '/interfacelist/list',
            {
                type: "GET",
                data: {
                    id: id
                },
                async: false,
                success: function (obj) {
                    // console.log(obj)
                    if (obj.data.length != 0) {
                        var paramArray = obj.data[0].paramId.split(",")
                        // console.log(paramArray)
                        var a = '<div class="layui-col-md12">';
                        a += '<div class="layui-col-md4">';
                        a += '<span>接口描述:</span>';
                        a += '<span>' + obj.data[0].description + '</span>';
                        a += '</div>';
                        if (deptId == 25) {
                            a += '<div class="layui-col-md4 layui-col-md-offset4 layui-inline">';
                            a += '<button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>';
                            a += '<button id="btnEdit" class="layui-btn icon-btn"><i class="layui-icon">&#xe642;</i>修改</button>';
                            a += '<button id="btnDelete" class="layui-btn icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>';
                            a += '</div>';
                        }
                        a += '<div class="layui-col-md12">';
                        a += '<span>接口地址:</span>';
                        a += '<span class="searchLoc">' + obj.data[0].loc + '</span>';
                        a += '</div>';
                        a += '</div>';
                        a += '<div class="layui-col-md12" style="margin-top: 60px">';
                        a += '<span class="layui-col-md12">参数说明</span>';
                        a += '<div class="layui-col-md7" style="margin-top: 15px">';
                        a += '<table class="layui-table">';
                        a += '<tr>';
                        a += '<td style="text-align: center;width: 35px">序号</td>';
                        a += '<td style="text-align: center">参数</td>';
                        a += '<td style="text-align: center">释义</td>';
                        a += '</tr>';
                        for (var i = 0; i < paramArray.length; i++) {
                            var paramId = parseInt(paramArray[i]);
                            $.ajax(Feng.ctxPath + '/interfaceparam/list?id=' + paramId, {
                                type: "GET",
                                async: false,
                                success: function (res) {
                                    // console.log(res)
                                    a += '<tr>';
                                    a += '<td style="text-align: center;width: 35px">' + (i + 1) + '</td>';
                                    a += '<td style="text-align: center">' + res.data[0].param + '</td>';
                                    a += '<td style="text-align: center">' + res.data[0].detail + '</td>';
                                    a += '</tr>';
                                }
                            })
                        }
                        a += '</table>';
                        a += '</div>';
                        a += '<div class="layui-col-md12" style="margin-top: 40px">';
                        a += '<span class="layui-col-md12">返回示例</span>';
                        a += '<textarea class="layui-col-md10" style="overflow-y:visible;border: none;background-color: white;resize:none" id="returnSample" disabled>' + obj.data[0].returnSample + '</textarea>';
                        a += '</div>';
                        a += '</div>';
                        $("#content").append(a)
                        $("#returnSample").val(obj.data[0].returnSample)
                        /**
                         * 文本域高度自适应
                         */
                        //jQuery实现textarea高度根据内容自适应
                        $.fn.extend({
                            txtaAutoHeight: function () {
                                return this.each(function () {
                                    var $this = $(this);
                                    if (!$this.attr('initAttrH')) {
                                    }
                                    setAutoHeight(this).on('input', function () {
                                        setAutoHeight(this);
                                    });
                                });

                                function setAutoHeight(elem) {
                                    var $obj = $(elem);
                                    return $obj.css({
                                        height: $obj.attr('initAttrH'),
                                        'overflow-y': 'hidden'
                                    }).height(elem.scrollHeight);
                                }
                            }
                        });
                        //调用
                        $(function () {
                            $("#returnSample").txtaAutoHeight();
                        });
                    }
                }
            })
    })


    /**
     * 点击查询按钮
     */
    $("#btnSearch").click(function () {
        var name = $("#name").val()
        $.ajax(Feng.ctxPath + '/interfacelist/list', {
            data: {
                name: name
            },
            type: 'GET',
            async: false,
            success: function (obj) {
                console.log(obj)
                $("#menu").empty()
                $("#content").empty()
                if (obj.data.length != 0) {
                    var data = obj.data;
                    for (var i = 0; i < data.length; i++) {
                        var a = '<li class="layui-nav-item layui-col-md12" id=' + data[i].id + ' style="border-bottom: silver solid 1px;border-right: silver solid 1px;">';
                        a += '<a href="javascript:;" style="color: black;font-size: 16px;text-align: center" id="interface">' + data[i].name + '</a>';
                        a += '</li>'
                        $("#menu").append(a)
                    }
                    var paramArray = obj.data[0].paramId.split(",")
                    // console.log(paramArray)
                    var b = '<div class="layui-col-md12">';
                    b += '<div class="layui-col-md4">';
                    b += '<span>接口描述:</span>';
                    b += '<span>' + obj.data[0].description + '</span>';
                    b += '</div>';
                    if (deptId == 25) {
                        b += '<div class="layui-col-md4 layui-col-md-offset4 layui-inline">';
                        b += '<button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>';
                        b += '<button id="btnEdit" class="layui-btn icon-btn"><i class="layui-icon">&#xe642;</i>修改</button>';
                        b += '<button id="btnDelete" class="layui-btn icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>';
                        b += '</div>';
                    }
                    b += '<div class="layui-col-md12">';
                    b += '<span>接口地址:</span>';
                    b += '<span class="searchLoc">' + obj.data[0].loc + '</span>';
                    b += '</div>';
                    b += '</div>';
                    b += '<div class="layui-col-md12" style="margin-top: 60px">';
                    b += '<span class="layui-col-md12">参数说明</span>';
                    b += '<div class="layui-col-md7" style="margin-top: 15px">';
                    b += '<table class="layui-table">';
                    b += '<tr>';
                    b += '<td style="text-align: center;width: 35px">序号</td>';
                    b += '<td style="text-align: center">参数</td>';
                    b += '<td style="text-align: center">释义</td>';
                    b += '</tr>';
                    for (var i = 0; i < paramArray.length; i++) {
                        var paramId = parseInt(paramArray[i]);
                        $.ajax(Feng.ctxPath + '/interfaceparam/list?id=' + paramId, {
                            type: "GET",
                            async: false,
                            success: function (res) {
                                // console.log(res)
                                b += '<tr>';
                                b += '<td style="text-align: center;width: 35px">' + (i + 1) + '</td>';
                                b += '<td style="text-align: center">' + res.data[0].param + '</td>';
                                b += '<td style="text-align: center">' + res.data[0].detail + '</td>';
                                b += '</tr>';
                            }
                        })
                    }
                    b += '</table>';
                    b += '</div>';
                    b += '<div class="layui-col-md12" style="margin-top: 40px">';
                    b += '<span class="layui-col-md12">返回示例</span>';
                    b += '<textarea class="layui-col-md10" style="overflow-y:visible;border: none;background-color: white;resize:none" id="returnSample" disabled>' + obj.data[0].returnSample + '</textarea>';
                    b += '</div>';
                    b += '</div>';
                    $("#content").append(b)
                    $("#returnSample").val(obj.data[0].returnSample)
                    /**
                     * 文本域高度自适应
                     */
                    //jQuery实现textarea高度根据内容自适应
                    $.fn.extend({
                        txtaAutoHeight: function () {
                            return this.each(function () {
                                var $this = $(this);
                                if (!$this.attr('initAttrH')) {
                                }
                                setAutoHeight(this).on('input', function () {
                                    setAutoHeight(this);
                                });
                            });

                            function setAutoHeight(elem) {
                                var $obj = $(elem);
                                return $obj.css({
                                    height: $obj.attr('initAttrH'),
                                    'overflow-y': 'hidden'
                                }).height(elem.scrollHeight);
                            }
                        }
                    });
                    //调用
                    $(function () {
                        $("#returnSample").txtaAutoHeight();
                    });
                }
            }
        })
    })

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    $("#content").on('click', '#btnDelete', function () {
        var loc = $(this).parent().parent().find(".searchLoc").text()
        // console.log(loc)
        $.ajax(Feng.ctxPath + '/interfacelist/list', {
            type: "GET",
            data: {
                loc: loc
            },
            success: function (obj) {
                console.log(obj)
                var id = obj.data[0].id
                console.log(id)
                Feng.confirm("是否删除?", function () {
                    $.ajax(Feng.ctxPath + '/interfacelist/delete', {
                        data: {
                            id: id
                        },
                        success: function () {
                            Feng.success("删除成功!");
                            window.location.reload();
                        },
                        error: function (data) {
                            Feng.error("删除失败!" + data.responseJSON.message + "!");
                        }
                    })
                });
            }
        })


    })


    /**
     * 弹出添加对话框
     */

    $("#content").on('click', '#btnAdd', function () {
        // window.location.href = Feng.ctxPath + "/interfacelist/add"
        layer.open({
            title: '',
            content: Feng.ctxPath + "/interfacelist/add",
            type: 2,
            area: ['80%', '80%'],
            maxmin: true,
            success: function (layero) {
                layero.find('.layui-layer-min').remove();
            },
        })
    })


    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    $("#content").on('click', '#btnEdit', function () {
        var loc = $(this).parent().parent().find(".searchLoc").text()
        // console.log(loc)
        $.ajax(Feng.ctxPath + '/interfacelist/list', {
            type: "GET",
            data: {
                loc: loc
            },
            success: function (obj) {
                // console.log(obj)
                var id = obj.data[0].id
                layer.open({
                    title: '',
                    area: ['80%', '80%'],
                    content: Feng.ctxPath + "/interfacelist/edit?id=" + id,
                    type: 2,
                    maxmin: true,
                    success: function (layero) {
                        layero.find('.layui-layer-min').remove();
                    },
                })
            }
        })
    })


    /**
     * 文本域高度自适应
     */
    //jQuery实现textarea高度根据内容自适应
    $.fn.extend({
        txtaAutoHeight: function () {
            return this.each(function () {
                var $this = $(this);
                if (!$this.attr('initAttrH')) {
                }
                setAutoHeight(this).on('input', function () {
                    setAutoHeight(this);
                });
            });

            function setAutoHeight(elem) {
                var $obj = $(elem);
                return $obj.css({height: $obj.attr('initAttrH'), 'overflow-y': 'hidden'}).height(elem.scrollHeight);
            }
        }
    });
    //调用
    $(function () {
        $("#returnSample").txtaAutoHeight();
    });


});
